package com.wk.controller.teacher;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wk.pojo.*;
import com.wk.service.Courseservice;
import com.wk.utils.PaginationConstant;
import com.wk.utils.responseresult;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @program: thesis
 * @description: 教师实现课程管理
 * @author: 王侃
 * @create: 2020-02-20 13:25
 **/
@Controller
@RequestMapping("/tea")
public class Teacoursecontroller {
    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    Courseservice courseservice;

    //返回课程的类型
    @ModelAttribute("coursetypes")
    public List<Coursetype> coursetypes()
    {
        List<Coursetype> coursetypes=courseservice.selectbyalltype();
        return coursetypes;
    }

    //跳转到课程发布界面
    @RequestMapping("/center/release_course")
    public String release_course()
    {
        return "teacher/release_course";
    }

    //提交发布课程
    @RequestMapping("/center/course/submit")
    public String course_submit(HttpSession session, String course_name, MultipartFile course_image, MultipartFile course_video, Integer coursetype_id)
    {
        logger.info("============>文件上传");
        //上传图片和视频并且获得路径
        String video=courseservice.upload(course_video);
        String image=courseservice.upload(course_image);
        User user=(User)session.getAttribute("loginuser");
        //封装成类
        Course_topic topic=new Course_topic();
        topic.setCourse_image(image);
        topic.setCourse_video(video);
        topic.setCourse_name(course_name);
        topic.setCoursetype_id(coursetype_id);
        topic.setCreatetime(new Timestamp(new Date().getTime()));
        topic.setUser_id(user.getId());
        topic.setUsername(user.getUsername());
        topic.setPraise(0);
        //存入数据库
        courseservice.insertintotopic(topic);
        return "common/success";
    }

    //跳转到课程习题添加界面
    @RequestMapping("/center/course_test/add")
    public String course_add(HttpSession session,Model model)
    {
        //获得该用户的所有课程
        User user=(User)session.getAttribute("loginuser");
        List<Course_topic> topics=courseservice.selectbytopiclist(user.getId(),"");
        model.addAttribute("tests",topics);
        return "teacher/course_test_add";
    }

    //课程习题完成添加进行提交
    @RequestMapping("/center/course_test/addsubmit")
    public String addsubmit(Course_exam exam,HttpSession session)
    {
        User user=(User)session.getAttribute("loginuser");

        exam.setUser_id(user.getId());
        exam.setType_id(1);//固定为单选题
        //存入数据库
        courseservice.insertintocourse_exam(exam);

        return "common/success";
    }

    //修改课程测试并且提交
    @RequestMapping("/center/course_test/updatesubmit")
    public String test_updatesubmit(Course_exam exam)
    {
        System.out.println(exam.getId());
        courseservice.updatebycourseexam(exam);
        return "common/success";
    }



    //修改课程习题前先显示出来,通过主键id显示课程测试
    @RequestMapping("/center/course_test/getinformation")
    public String test_information(String id,Model model,HttpSession session)
    {
        Integer ID=Integer.parseInt(id);
        Course_exam exam=courseservice.selectbyid(id);
        model.addAttribute("exam",exam);

        //获得该用户的所有课程
        User user=(User)session.getAttribute("loginuser");
        List<Course_topic> topics=courseservice.selectbytopiclist(user.getId(),"");
        model.addAttribute("tests",topics);
        return "teacher/coursetest_update";
    }

    //跳转到课程习题管理界面
    @RequestMapping("/center/course_test/list")
    public String course_list(Integer page, HttpSession session, Model model)
    {
        User user=(User)session.getAttribute("loginuser");

        //使用分页插件
        //对第一次传值进行判断
        if (ObjectUtils.isEmpty(page)) {
            page = PaginationConstant.CURRENT_NUM;
        }
        //设置分页
        PageHelper.startPage(page, PaginationConstant.PAGE_SIZE);
        List<Course_exam> exams=courseservice.selectbyuser_exam(user.getId());
        PageInfo<Course_exam> pageInfo=new PageInfo<>(exams);
        model.addAttribute("pageinfo", pageInfo);
        //获得该用户的所有课程
        List<Course_topic> topics=courseservice.selectbytopiclist(user.getId(),"");
        model.addAttribute("tests",topics);
        return "teacher/course_test";
    }

    //显示已发布课程的管理
    @RequestMapping("/center/coursetopic/list")
    public String coursetopic_list(String course_name,Integer page,HttpSession session, Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        //使用分页插件
        //        对第一次传值进行判断
        if (ObjectUtils.isEmpty(page)) {
            page = PaginationConstant.CURRENT_NUM;
        }
        //设置分页
        PageHelper.startPage(page, PaginationConstant.PAGE_SIZE);

        //不仅要传入当前页的值，还要传入查找的值
        List<Course_topic> list=courseservice.selectbytopiclist(user.getId(),course_name);
        PageInfo<Course_topic> pageInfo=new PageInfo<>(list);

        model.addAttribute("pageinfo", pageInfo);
        model.addAttribute("course_name",course_name);

        return "teacher/coursetopic_management";
    }

    //在更新课程信息时，通过主键id显示课程信息
    @RequestMapping("/center/coursetopic/getinformation")
    @ResponseBody
    public responseresult getinformation(String id)
    {
        Integer ID=Integer.parseInt(id);
        //获取到信息
        Course_topic topic=courseservice.selectbytopicall(ID);
        return responseresult.success(topic);
    }

    //更新课程信息实现上传
    @RequestMapping("/center/coursetopic/update")
    public String topicupdate(HttpSession session,String id,String course_name, MultipartFile course_image, MultipartFile course_video, Integer coursetype_id)
    {
        Integer ID=Integer.parseInt(id);

        //在阿里云的oss删除对应的文件
        Course_topic topic2=courseservice.selectbytopicall(ID);
        courseservice.deletealiyunoss(topic2.getCourse_image());
        courseservice.deletealiyunoss(topic2.getCourse_video());

        //上传图片和视频到阿里云oss并且获得路径
        String video=courseservice.upload(course_video);
        String image=courseservice.upload(course_image);

        User user=(User)session.getAttribute("loginuser");
        //封装成类
        Course_topic topic=new Course_topic();
        topic.setId(ID);
        topic.setCourse_image(image);
        topic.setCourse_video(video);
        topic.setCourse_name(course_name);
        topic.setCoursetype_id(coursetype_id);
        topic.setCreatetime(new Timestamp(new Date().getTime()));
        topic.setUser_id(user.getId());
        topic.setUsername(user.getUsername());

        //数据库更新
        courseservice.updatebycoursetopic(topic);
        return "common/success";
    }

    //播放自己的视频
    @RequestMapping("/center/mycourse_video")
    public String mycourse_video(String video,Model model)
    {
        model.addAttribute("video",video);
        return "common/mycourse_video";
    }

    //通过主键id删除课程习题
    @RequestMapping("/center/course_test/delete")
    public String test_delete(String id)
    {
        Integer ID=Integer.parseInt(id);
        courseservice.deletebycoursetestid(ID);
        return "common/success";
    }

    //通过主键id删除课程
    @RequestMapping("/center/coursetopic/delete")
    public String delete_coursetopic(String id)
    {
        Integer ID=Integer.parseInt(id);

        //在阿里云的oss删除对应的文件
        Course_topic topic=courseservice.selectbytopicall(ID);
        courseservice.deletealiyunoss(topic.getCourse_image());
        courseservice.deletealiyunoss(topic.getCourse_video());

        //删除该课程的随堂测试
        courseservice.deletebycourseexam(ID);
        //删除该课程的topic内容
        courseservice.deletebycoursetopic(ID);
        return "common/success";
    }

    //跳转到pdf管理界面
    @RequestMapping("/course/pdf/management")
    public String pdf_list(HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        List<Course_data> datas=courseservice.selectbyuserid_data(user.getId());
        model.addAttribute("datas",datas);
        List<Course_topic> topics=courseservice.selectbytopiclist(user.getId(),"");
        model.addAttribute("topics",topics);
        return "teacher/course/coursepdf_management";
    }

    //跳转到pdf添加界面
    @RequestMapping("/course/pdf/add")
    public String pdf_add(HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        List<Course_topic> topics=courseservice.selectbytopiclist(user.getId(),"");
        model.addAttribute("topics",topics);
        return "teacher/course/pdf_add";
    }

    //pdf文件上传
    @RequestMapping("/course/pdf/addsubmit")
    public String pdf_addsubmit(HttpSession session,Course_data data,MultipartFile pdf_dto)
    {
        logger.info("============>文件上传");
        User user=(User)session.getAttribute("loginuser");
        //上传图片和视频并且获得路径
        String pdf=courseservice.upload(pdf_dto);

        data.setPdf(pdf);
        data.setUser_id(user.getId());
        courseservice.insertintocourse_data(data);
        return "common/success";
    }
}
